<template>
  <div class="search-div">
    <!-- 搜索表单 -->
    <el-form label-width="70px" size="small">
      <el-form-item label="角色名称">
        <el-input
          style="width: 100%"
          placeholder="角色名称"
          v-model="queryDto.roleName"
        ></el-input>
      </el-form-item>
      <el-row style="display: flex">
        <el-button type="primary" size="small" @click="searchSysRole">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>

    <!-- 添加按钮 -->
    <div class="tools-div">
      <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>

    <!-- 添加角色表单对话框 -->
    <el-dialog v-model="dialogVisible" title="添加或修改角色" width="30%">
      <el-form label-width="120px">
        <el-form-item label="角色名称">
          <el-input v-model="sysRole.roleName" />
        </el-form-item>
        <el-form-item label="角色Code">
          <el-input v-model="sysRole.roleCode" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="sysRole.description" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--- 角色表格数据 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="roleName" label="角色名称" width="180" />
      <el-table-column prop="roleCode" label="角色code" width="180" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="primary" size="small" @click="editShow(scope.row)">
          修改
        </el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row.id)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <!--分页条-->
    <el-pagination
      v-model:current-page="pageParam.page"
      v-model:page-size="pageParam.limit"
      :page-sizes="[2, 5, 10, 20]"
      @size-change="fetchData"
      @current-change="fetchData"
      layout="total, sizes, prev, pager, next"
      :total="total"
    />
  </div>
</template>

<script setup>
import {
  deleteSysRoleById,
  GetSysRoleListByPage,
  SaveSysRole,
  UpdateSysRole,
} from '@/api/sysRole'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, onMounted } from 'vue'
//角色删除-------
const deleteById = roleId => {
  //删除确认提示框
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async() => {
        const{code,message,data}=await deleteSysRoleById(roleId)
        if(code === 200){
            ElMessage.success("删除成功")
            fetchData()
        }
    })
}
//角色修改--------
//定义控制修改表单的变量
const editShow = row => {
  //显示修改对话框
  dialogVisible.value = true
  sysRole.value = row
}

// 角色新增-------
//定义角色数据模型
const defaultForm = {
  id: '',
  roleName: '',
  roleCode: '',
  description: '',
}
//设置为响应式
const sysRole = ref(defaultForm)
//保存事件方法
const submit = async () => {
  //判断数据模型中是否存在id 存在:修改 不存在：新增
  if (!sysRole.value.id) {
    //调用保存role数据api
    const { code, message, data } = await SaveSysRole(sysRole.value)
    //判断结果
    if (code === 200) {
      //提示成功关闭对话框
      ElMessage.success('操作成功')
      dialogVisible.value = false
      //刷新页面
      fetchData()
    }
  } else {
    //修改
    const { code, message, data } = await UpdateSysRole(sysRole.value)
    //判断结果
    if (code === 200) {
      ElMessage.success('修改成功')
    }
  }
}
// 控制对话是否展示的变量
const dialogVisible = ref(false)
//进入添加
const addShow = () => {
  dialogVisible.value = true
}
//定义重置方法
const resetData = () => {
  //去除条件内容
  queryDto.value = {}
  fetchData()
}
//定义搜索事件方法
const searchSysRole = () => {
  fetchData()
}
const queryDto = ref({ roleName: '' })
//封装分页数据
const defaultPageForm = {
  page: 1,
  limit: 10,
}
const pageParam = ref(defaultPageForm)
const fetchData = async () => {
  //获取角色列表数据
  const { code, message, data } = await GetSysRoleListByPage(
    pageParam.value.page,
    pageParam.value.limit,
    queryDto.value
  )
  if (code == 200) {
    total.value = data.total
    list.value = data.list
  }
}

//定义页面挂载后发起请求
onMounted(async () => {
  fetchData()
})
// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>